package com.mzplayer.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.mzplayer.utils.RS;
import com.mzplayer.utils.Util;
import com.mzplayer.videoview.base.StandardParent;

import java.util.HashMap;
import java.util.Map;

@SuppressLint("ViewConstructor")
public class ShowContainer extends FrameLayout {
    private final Context context = getContext();
    private final StandardParent standardParent;
    private final int optionSize;
    private final ColorStateList colorStateList;
    private final float textSize;
    private final int backgroundResource;
    private final Map<View, View> options = new HashMap<>();

    public ShowContainer(StandardParent standardParent) {
        super(standardParent.getContext());
        optionSize = (int) getResources().getDimension(RS.dimen.option_size);
        colorStateList = getResources().getColorStateList(RS.color.mz_selector_option_text);
        textSize = getResources().getDimension(RS.dimen.option_font_size);
        backgroundResource = RS.color.ctl_bg_alpha;
        standardParent.addView(this,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setVisibility(GONE);
        this.standardParent = standardParent;
    }

    private void startAnimation(int res) {
        Animation animation = AnimationUtils.loadAnimation(context, res);
        startAnimation(animation);
    }

    private void clearParent(View v) {
        ViewGroup parent = (ViewGroup) v.getParent();
        if (null != parent) {
            parent.removeView(v);
        }
    }

    public void show(View key) {
        View view = options.get(key);
        if (view != null) {
            removeAllViews();
            addView(view);
            startAnimation(RS.anim.mz_right_show);
            standardParent.hide();
            setVisibility(VISIBLE);
            Util.requestFocus(this);
        }
    }

    public void hide() {
        startAnimation(RS.anim.mz_right_hide);
        setVisibility(GONE);
        clearFocus();
        Util.requestFocus(standardParent);
    }

    private void addKey(ViewGroup keyContainer, int keyLeftMargin, View keyView) {
        clearParent(keyView);
        ViewGroup.MarginLayoutParams marginLayoutParams;
        if (!(keyView instanceof Button)&&(keyView instanceof TextView)) {
            marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView textView = (TextView) keyView;
            textView.setTextColor(colorStateList);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        } else {
            marginLayoutParams = new ViewGroup.MarginLayoutParams(optionSize, optionSize);
        }
        marginLayoutParams.leftMargin = keyLeftMargin;
        keyView.setLayoutParams(marginLayoutParams);
        keyContainer.addView(keyView);
    }

    public void addOption(ViewGroup keyContainer, int keyLeftMargin, Object key, Object value) {
        View keyView = Util.getView(key);
        if (keyView == null) {
            return;
        }
        if (value == null) {
            addKey(keyContainer, keyLeftMargin, keyView);
            return;
        }
        View valueView = Util.getView(value);
        if (valueView == null) {
            return;
        }
        clearParent(valueView);
        valueView.setBackgroundResource(backgroundResource);
        ViewGroup.LayoutParams vParams = valueView.getLayoutParams();
        int width=vParams!=null?vParams.width:ViewGroup.LayoutParams.WRAP_CONTENT;
        LayoutParams params = new LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.END;
        valueView.setLayoutParams(params);
        keyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show(v);
            }
        });
        options.put(keyView, valueView);
        addKey(keyContainer, keyLeftMargin, keyView);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (Util.isClick(event)) {
            hide();
        }
        return super.onInterceptTouchEvent(event);
    }
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (Util.isClick(event)) {
            hide();
        }
        return true;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            hide();
            return true;
        }
        return false;
    }
}
